<?php

namespace App\Http\Controllers\API\Admin;

use App\Http\Services\UtilService;
use App\Models\AppConfigModel;
use App\Models\CityModel;
use App\Models\UserModel;
use App\Exceptions\ResponseException;

use App\Http\Controllers\AppBaseController;

use Illuminate\Http\Response;
use Validator;
use Log;
use DB;

/**
 * Class V1Controller
 * @package App\Http\Controllers\API
 */
class V1Controller extends AppBaseController
{
    /**
     * 参数验证规则
     * self::VALID_RULES[__FUNCTION__]
     */
    const VALID_RULES = [];

    /**
     * 获取Appkey
     */
    public function getAppKey()
    {
        $key = $this->request->get('key');
        if (empty($key)) {
            $this->error(1, '参数不存在');
        }
        $value = (string) AppConfigModel::findOneByKey($key);
//        if (in_array($key, ['keywords', 'ticket_type'])) {
//            $value = json_decode($value, true);
//        }
        $value = @json_decode($value, true);
        empty($value) && $value = "";
        return $this->success(['value' => $value]);
    }

    /**
     *
     * 设置Appkey
     */
    public function setAppKey()
    {
        $key = $this->request->get('key');
        if (empty($key)) {
            $this->error(1, '参数不存在');
        }

        $value = trim($this->request->get('value'));
        if (empty($value)) {
            $this->error(1, 'value参数不存在');
        }

        $config =  AppConfigModel::where('key', $key)->first();
        $config->value = $value;
        $config->save();

        return $this->sendResponse([]);
    }

    /**
     * 获取地区json
     */
    public function getAddressTree()
    {
        $cities = DB::connection('mysql::read')->select('select `id`, `city` as name, `parent_id` from cities');
        $cities = array_map(function ($value) {
            return (array) $value;
        }, $cities);
        $provices = DB::connection('mysql::read')->select('select `provinceid` as id, `province` as name from provinces');
        $arr = [];

        foreach ($provices as $provice) {
            $proviceId = $provice->id;
            $ret['id'] = (int) $proviceId;
            $ret['name'] = $provice->name;
            $ret['children'] = parentSons($proviceId, $cities);

            $arr[] = $ret;
        }

        return $this->success($arr);
    }

    /**
     * 获取地区list
     */
    public function getCityList()
    {
        $cities = DB::connection('mysql::read')->select('select `id`, `city` as name, `parent_id` from cities');
        $cities = array_map(function ($value) {
            return (array) $value;
        }, $cities);

        return $this->success($cities);
    }

    /**
     * 搜索城市返回ID
     * @return \Illuminate\Http\JsonResponse
     */
    public function searchCity()
    {
        $keywords = trim($this->request->get('name', ''));
        $keywords = addslashes($keywords);
        Log::info("模糊匹配城市" . $keywords);
        if (empty($keywords)) {
            return $this->success(['city_id' => 0]);
        }
        $city = DB::table('cities')
            ->where('city', 'like', '%' . $keywords . '%')
            ->first();

        if (empty($city)) {
            return $this->success(['city_id' => 0]);
        } else {
            return $this->success(['city_id' => $city->id]);
        }
    }

    /**
     * 搜索城市返回ID
     * @return \Illuminate\Http\JsonResponse
     */
    public function searchCityByTicket()
    {
        $cityIds = DB::table('ticket')
            ->select('city_id')
            ->where('status', 0)
            ->distinct()
            ->get()->toArray();

        if (empty($cityIds)) {
            return $this->success([]);
        }

        $ids = array_column($cityIds, 'city_id');
        $city = DB::table('cities')
            ->select(DB::raw('id, city as name'))
            ->whereIn('id', $ids)
            ->get()
            ->toArray();

        if (empty($city)) {
            return $this->success([]);
        } else {
            return $this->success($city);
        }
    }
}
